package com.tabjin.rfidsocket.controller.v1.access;

import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.exception.ParamException;
import com.tabjin.rfidsocket.pojo.ArchBuilding;
import com.tabjin.rfidsocket.pojo.ArchProjectBuildingUser;
import com.tabjin.rfidsocket.pojo.bo.access.ArchBuildingBO;
import com.tabjin.rfidsocket.pojo.vo.access.ArchBuildingVO;
import com.tabjin.rfidsocket.service.access.ArchBuildingService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author tabjin
 * create at 2021/1/14 15:17
 * @program rfid_socket
 * @description 楼栋
 */
@RestController
@RequestMapping(value = "/access/building")
public class BuildingController {

    private final ArchBuildingService archBuildingService;

    public BuildingController(ArchBuildingService archBuildingService) {
        this.archBuildingService = archBuildingService;
    }

    @RequiresPermissions(value = {"/access/building/add"}, logical = Logical.OR)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody ArchBuildingBO archBuildingBO) {
        archBuildingService.add(archBuildingBO);
        return TabJsonResult.ok();
    }

    /**
     * 删除楼栋
     *
     * @param buildingId
     * @return
     */
    @RequiresPermissions(value = {"/access/building/delete"}, logical = Logical.OR)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestParam(value = "id", required = true) Integer buildingId) {
        archBuildingService.delete(buildingId);
        return TabJsonResult.ok();
    }

    @RequiresPermissions(value = {"/access/building/update"}, logical = Logical.OR)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody ArchBuildingBO archBuildingBO) {
        ArchBuildingVO result = archBuildingService.update(archBuildingBO);
        return TabJsonResult.ok(result);
    }

    /**
     * 获取单个楼栋d
     *
     * @param buildingId
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getItem(@RequestParam(value = "id", required = true) Integer buildingId) {
        Optional<ArchBuilding> item = archBuildingService.getItem(buildingId);
        ArchBuilding archBuilding = item.orElseThrow(() -> new ParamException("获取楼栋失败"));
        return TabJsonResult.ok(archBuilding);
    }

    /**
     * 获取所有楼栋
     *
     * @param
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult getAllBuildings(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult result = archBuildingService.getAll(page, pageSize);
        return TabJsonResult.ok(result);
    }

    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/by/project/part", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult getBuildingsByProjectId(@RequestParam(value = "projectId", required = true) Integer projectId,
                                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return TabJsonResult.ok(archBuildingService.getBuildingsByProjectId(projectId, page, pageSize));
    }

    /**
     * 获取楼栋下人员
     * @param buildingId
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult getBuildingUsers(@RequestParam(value = "buildingId", required = true) Integer buildingId) {
        return TabJsonResult.ok(archBuildingService.getBuildingUsers(buildingId));
    }

    /**
     * 根据项目获取楼栋信息
     *
     * @param projectId
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/by/project/all", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getBuildingsByProjectIdWithoutPage(@RequestParam(value = "projectId", required = true) Integer projectId) {
        List<ArchBuilding> buildingList = archBuildingService.getBuildingsByProjectId(projectId);
        return TabJsonResult.ok(buildingList);
    }

    /**
     * 根据用户获取楼栋列表
     *
     * @param userId
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/by/user/all", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getBuildingsByUserIdWithoutPage(@RequestParam(value = "projectId", required = false) Integer projectId,
                                                         @RequestParam(value = "userId", required = true) Integer userId) {
        Optional<List<ArchBuildingVO>> archBuildingVOOptional = archBuildingService.searchItemsByProjectIdAndUserId(projectId, userId);
        List<ArchBuildingVO> archBuildingList = archBuildingVOOptional.orElseThrow(() -> new ParamException("无楼栋"));
        return TabJsonResult.ok(archBuildingList);
    }

    /**
     * 楼栋产权性质
     *
     * @return
     */
    @RequiresPermissions(value = {"/access/building/list"}, logical = Logical.OR)
    @RequestMapping(value = "/property_right", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult propertyRight() {
//        ArchBuildingVO result = archBuildingService.getpropertyRight();
        return TabJsonResult.ok();
    }

}
